package cn.hr.service.impl;

import java.io.File;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import cn.hr.dao.mapper.MissionTypeMapper;
import cn.hr.pojo.MissionType;
import cn.hr.utils.Constants;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import cn.hr.dao.mapper.TrainMapper;
import cn.hr.pojo.Train;
import cn.hr.service.TrainService;

/**
 * 培训服务接口实现类
 *
 * @author MO
 */
@CacheConfig(cacheNames = "train")
@Scope("prototype")
@Service("trainService")
public class TrainServiceImpl implements TrainService {
    @Resource
    private TrainMapper trainMapper;

    @Resource
    private MissionTypeMapper missionTypeMapper;

    @Override
    public boolean insert(Train t) throws Exception {
        boolean flag=false;
        Integer id = trainMapper.findMaxId() + 1;
        if (trainMapper.insert(t) == 1){
            MissionType missionType=new MissionType();
            missionType.setTypeId(12);
            missionType.setTableId(id);
            missionType.setCreatedDate(new Date());
            missionType.setCreatedById(t.getCreatedById());
            missionType.setName("培训審核");
            missionTypeMapper.insert(missionType);
            flag=true;
        }
        return flag;
    }

    @Override
    public boolean update(Train t) throws Exception {
        return trainMapper.update(t) == 1;
    }

    @Override
    public boolean delete(Integer id) throws Exception {
        return trainMapper.delete(id) == 1;
    }

    @Override
    public Train get(Integer id) throws Exception {
        return trainMapper.get(id);
    }


    @Override
    public PageInfo<Map> findListByCondition(Map m, int pageNo) throws Exception {
        PageHelper.startPage(pageNo, Constants.PAGESIZE);
        List<Map> maps = trainMapper.findListByCondition(m);
        PageInfo<Map> pageInfo = new PageInfo<>(maps);
        return pageInfo;
    }

    @Override
    public Long findCountByCondition(Map m) throws Exception {
        return trainMapper.findCountByCondition(m);
    }

    @Override
    public boolean delFile(Integer id) throws SQLException {
        Train train=trainMapper.selectPath(id);
        File file = new File(train.getTrainData());
        if (file.exists()){
            file.delete();
        }
        return trainMapper.delFile(id)==1;
    }

    @Override
    public boolean feedback(Train train) throws SQLException {
        return trainMapper.feedback(train)>0;
    }

    @Override
    public Train selectPath(@Param("id") Integer id) throws SQLException {
        return trainMapper.selectPath(id);
    }
}
